﻿using UnityEngine;
using System.Collections;
using System.Threading;

public class GameMessageHeader
{
    private static int CLIENT_SEQ_ID = 0;
    public const int HEADER_LENGTH = 25;
    public int MessageSize { set; get; }
    public int ClientSeqID { get; set; }
    public int MessageId { get; set; }
    public long ClientSendTime { get; set; }
    public long ServerSendTime { get; set; }

    public EnumMessageType MessageType { get; set; }

    public int ErrorCode { get; set; }
    public int Version { get; set; }
    public int Compress { get; set; }
    public int ServiceId { get; set; }

    public void Write(ByteBuf byteBuf)
    {
        ClientSeqID = Interlocked.Increment(ref CLIENT_SEQ_ID);
        byteBuf.WriteInt(ClientSeqID);
        byteBuf.WriteInt(MessageId);
        byteBuf.WriteInt(ServiceId);
        byteBuf.WriteByte((int)MessageType);
        ClientSendTime = DateUtil.CurrentMillTime();
        byteBuf.WriteLong(ClientSendTime);
        byteBuf.WriteInt(Version);

    }

    public void Read(ByteBuf byteBuf)
    {
        int messageId = byteBuf.ReadInt();
        Read(byteBuf, messageId);
    }
    public void Read(ByteBuf byteBuf, int messageId)
    {
        MessageId = messageId;
        ServerSendTime = byteBuf.ReadLong();
        Version = byteBuf.ReadInt();
        ErrorCode = byteBuf.ReadInt();
    }
}
